﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.inventory {
    import Engine.Classes.*;
    import flash.utils.*;
    import Display.DialogUI.*;
    import Classes.*;
    import Display.*;
    import GameMode.*;

    public class Commodities extends BaseInventory {

        public static const GOODS_COMMODITY:String = "goods";
        public static const PREMIUM_GOODS_COMMODITY:String = "premium_goods";

        protected var m_capacities:Dictionary;

        public function Commodities(_arg1:Object){
            this.m_capacities = new Dictionary();
            this.updateCapacities();
            super(_arg1);
        }
        protected static function isCommodityStorage(_arg1:WorldObject):Boolean{
            var _local2:MapResource;
            if ((Global.world.getTopGameMode() is GMPlaceMapResource)){
                _local2 = (Global.world.getTopGameMode() as GMPlaceMapResource).displayedMapResource;
            };
            return ((((((_arg1 is Pier)) && (!((_arg1 == _local2))))) || ((((_arg1 is Storage)) && (!((_arg1 == _local2)))))));
        }
        public static function getNoCommoditiesTooltip():String{
            var _local1 = "";
            _local1 = ZLoc.t("Main", "NoGoods");
            return (_local1);
        }

        override public function isValidName(_arg1:String):Boolean{
            var _local2:XML = Global.gameSettings.getCommodityXMLByName(_arg1);
            return (!((_local2 == null)));
        }
        override public function getCapacity(_arg1:String):int{
            return (int(this.m_capacities[_arg1]));
        }
        public function updateCapacities():void{
            var _local3:MapResource;
            var _local4:XMLList;
            var _local5:XML;
            var _local6:Boolean;
            var _local7:String;
            var _local8:XML;
            var _local9:String;
            var _local10:int;
            var _local1:Dictionary = this.m_capacities;
            this.m_capacities = new Dictionary();
            var _local2:Array = Global.world.getObjectsByPredicate(Commodities.isCommodityStorage);
            for each (_local3 in _local2) {
                _local8 = _local3.getItem().commodityXml;
                if (_local8){
                    _local9 = _local8.@name;
                    if (this.m_capacities[_local9] == null){
                        this.m_capacities[_local9] = 0;
                    };
                    _local10 = int(_local8.@capacity);
                    this.m_capacities[_local9] = (this.m_capacities[_local9] + _local10);
                };
            };
            _local4 = Global.gameSettings.getCommodityXML();
            for each (_local5 in _local4) {
                _local9 = String(_local5.@name);
                if (this.m_capacities[_local9] == null){
                    this.m_capacities[_local9] = 0;
                };
                if (_local5.@initCap){
                    this.m_capacities[_local9] = (this.m_capacities[_local9] + parseInt(_local5.@initCap));
                };
                if (((((m_storage) && (m_storage[_local9]))) && ((m_storage[_local9] > this.getCapacity(_local9))))){
                    m_storage[_local9] = this.getCapacity(_local9);
                };
            };
            _local6 = false;
            for (_local7 in this.m_capacities) {
                if (_local1.hasOwnProperty(_local7)){
                    if (_local1[_local7] != this.m_capacities[_local7]){
                        _local6 = true;
                        break;
                    };
                } else {
                    _local6 = true;
                    break;
                };
            };
            if (_local6){
                this.onDataChange(_local9);
            };
        }
        override protected function onDataChange(_arg1:String):void{
            Global.hud.updateCommodities();
            Global.player.commodityNotifyObservers(_arg1);
        }
        public function getCommodityNames():Array{
            var _local2:String;
            var _local1:Array = new Array();
            for (_local2 in this.m_capacities) {
                _local1.push(_local2);
            };
            return (_local1);
        }
        override public function add(_arg1:String, _arg2:int):Boolean{
            if (((!(this.isValidName(_arg1))) || ((_arg2 <= 0)))){
                return (false);
            };
            if (m_storage[_arg1] == null){
                m_storage[_arg1] = 0;
            };
            m_storage[_arg1] = (m_storage[_arg1] + _arg2);
            if (m_storage[_arg1] > this.getCapacity(_arg1)){
                m_storage[_arg1] = this.getCapacity(_arg1);
            };
            this.onDataChange(_arg1);
            return (true);
        }
        public function isAtCommodityCapacity(_arg1:Item, _arg2:Boolean=true):Boolean{
            var _local6:String;
            var _local7:StorageDialog;
            var _local3:XML = _arg1.commodityXml;
            var _local4:String = _local3.@name;
            var _local5:int = (Global.isVisiting()) ? Global.gameSettings.getInt("friendHelpDefaultGoodsReward", 1) : (parseInt(_local3.@produces) + Global.player.GetDooberMinimums(_arg1, _local4));
            if ((((this.getCapacity(_local4) <= getCount(_local4))) || ((getSpareCapacity(_local4) < _local5)))){
                if (_local4 == Commodities.PREMIUM_GOODS_COMMODITY){
                    _local6 = ZLoc.t("Dialogs", "MaxPremiumGoodsDialogText");
                    UI.displayMessage(_local6);
                } else {
                    if (_arg2){
                        _local7 = new StorageDialog(StorageDialog.TYPE_NEED_MORE_STORAGE);
                        UI.displayPopup(_local7);
                    };
                };
                return (true);
            };
            return (false);
        }

    }
}//package Classes.inventory 
